package net.wangds.auth.application.ctrl;

import net.wangds.auth.module.ResourceSchemaEnum;
import net.wangds.auth.resource.comp.GeneralResourceComp;
import net.wangds.auth.resource.entity.GeneralResourceEntity;
import net.wangds.common.base.funcs.Functions;
import net.wangds.jpa.Sorts;
import net.wangds.jpa.comp.AbstractRemovedComp;
import net.wangds.jpa.ctrl.AbstractJpaCompCtrl;
import net.wangds.web.entrypoint.BaseMultipleResp;
import net.wangds.web.entrypoint.PagingForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/auth/application")
public class ApplicationCtrl extends AbstractJpaCompCtrl<GeneralResourceEntity> {



    @Autowired
    private GeneralResourceComp mmCmpRes;



    @Override
    protected AbstractRemovedComp<GeneralResourceEntity> fetchJpaComp() {
        return mmCmpRes;
    }

    @Override
    protected BaseMultipleResp<GeneralResourceEntity> listWithKeywords(PagingForm page) {
        return mmConvPage2Resp.convert(mmCmpRes.findByKeywords(page.getKeywords(), mmConvPf2Pr.convert(page)));
    }

    @RequestMapping(value = "/init", method = RequestMethod.PUT)
    public BaseMultipleResp<GeneralResourceEntity> init() {

        //删除现有应用
        mmCmpRes.deleteInBatch(
            mmCmpRes.findBySchema(ResourceSchemaEnum.APPLICATION, Sorts.createTm_asc)
        );


        Functions.ofList(
                //GeneralResourceEntity.ofApplication("cbg_android", "藏宝楼应用(android版)", "", "藏宝楼应用的Android版本", "1", "", 0)
                //,GeneralResourceEntity.ofApplication("cbg_ios", "藏宝楼应用(iOS版)", "", "藏宝楼应用的iOS版本", "1", "", 0)
                //,GeneralResourceEntity.ofApplication("business", "商户平台", "", "藏宝楼商户后台", "1", "", 0)
                //,GeneralResourceEntity.ofApplication("plateform", "运营平台", "", "藏宝楼运营平台", "1", "", 0)
                //,
                GeneralResourceEntity.ofApplication("console", "管理后台", "", "管理后台", "1", "", 0)
                ,GeneralResourceEntity.ofApplication("maintain", "运维平台", "", "运维平台", "1", "", 0)

        ).forEach(
                mmCmpRes::save
        );



        return BaseMultipleResp.list(
                mmCmpRes.findBySchema(ResourceSchemaEnum.APPLICATION, Sorts.createTm_asc)
        );
    }

}
